<?php
/**
 * There definitely needs to be a better way to create the MBS book Object;
 * 
 * @author Kartik
 *
 */
class Buyback_Resource_Offer_MBS implements SeekableIterator, Countable, ArrayAccess
{
	const NOT_AVAILABLE = 1;
	
	const OTHER = "undefined error";
		
	protected $_items = array();
	
	public function getOffer($isbnList){
		
		if(null == $isbnList){
			return false;
		}
		
		//There is a better way to do this but for the time being this should suffice
		foreach($isbnList as $isbn){
			$isbnArray = explode(',', $isbn);
			foreach($isbnArray as $k => $isbn){
				try{
					$item = Doctrine_Model_MbsList::fetchBook($isbn);
					$offerItem = new Buyback_Resource_Offer_Item(
									'M',
									$item,
									Doctrine_Model_PriceRulesMbs::fetchOfferMBS(array("price"=>($item->guide_price*100))),
									$item->activity_code
					);
					$this->_items[$offerItem->Isbn13] = $offerItem;
				}catch(Exception $e){
					$this->_items[$isbn] = null;
				}
			}
		}
		return $this->_items;
	}

	/**
     * Does the given offset exist?
     *
     * @param string|int $key key
     * @return boolean offset exists?
     */
    public function offsetExists($key)
    {
        return isset($this->_items[$key]);
    }

    /**
     * Returns the given offset.
     *
     * @param string|int $key key
     * @return mixed
     */
    public function offsetGet($key)
    {
        return $this->_items[$key];
    }

    /**
     * Sets the value for the given offset.
     *
     * @param string|int $key
     * @param mixed $value
     */
    public function offsetSet($key, $value)
    {
        return $this->_items[$key] = $value;
    }

    /**
     * Unset the given element.
     *
     * @param string|int $key
     */
    public function offsetUnset($key)
    {
        unset($this->_items[$key]);
    }

    /**
     * Returns the current row.
     *
     * @return array|boolean current row 
     */
    public function current()
    {
        return current($this->_items);
    }

    /**
     * Returns the current key.
     *
     * @return array|boolean current key
     */
    public function key()
    {
        return key($this->_items);
    }

    /**
     * Moves the internal pointer to the next item and
     * returns the new current item or false.
     *
     * @return array|boolean next item
     */
    public function next()
    {
        return next($this->_items);
    }

    /**
     * Reset to the first item and return.
     *
     * @return array|boolean first item or false
     */
    public function rewind()
    {
        return reset($this->_items);
    }

    /**
     * Is the pointer set to a valid item?
     *
     * @return boolean valid item?
     */
    public function valid()
    {
        return current($this->_items) !== false;
    }

    /**
     * Seek to the given index.
     *
     * @param int $index seek index
     */
    public function seek($index)
    {
        $this->rewind();
        $position = 0;

        while ($position < $index && $this->valid()) {
            $this->next();
            $position++;
        }

        if (!$this->valid()) {
            throw new Exception('Invalid seek position');
        }
    }

    /**
     * Count the cart items
     *
     * @return int row count
     */
    public function count()
    {
        return count($this->_items);
    }
}